/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.railway.reports;

import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
import net.sf.dynamicreports.examples.Templates;
import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
import static net.sf.dynamicreports.report.builder.DynamicReports.*;
import net.sf.dynamicreports.report.builder.column.TextColumnBuilder;
import net.sf.dynamicreports.report.builder.group.ColumnGroupBuilder;
import net.sf.dynamicreports.report.builder.style.StyleBuilder;
import net.sf.dynamicreports.report.constant.HorizontalAlignment;
import net.sf.dynamicreports.report.constant.VerticalAlignment;
import net.sf.dynamicreports.report.datasource.DRDataSource;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.swing.JRViewer;

/**
 *
 * @author Kenny
 */
public class ScheduleReport {

    private JRViewer reportView;
    private JRDataSource datasource;

    public void buildData(ArrayList<Map<String, Object>> reportData) {
        datasource = buildDataSource(reportData);
    }

    public JRViewer buildException() {
        try {
            StyleBuilder titleStyle = stl.style(Templates.boldCenteredStyle)
                    .setVerticalAlignment(VerticalAlignment.MIDDLE)
                    .setFontSize(13);
            JasperReportBuilder report = new JasperReportBuilder();
            TextColumnBuilder scheNameCol = col.column("Schedule Name", "scheduleName", type.stringType());
            TextColumnBuilder monthCol = col.column("Month", "month", type.stringType());
            TextColumnBuilder originCol = col.column("Origin", "origin", type.stringType());
            TextColumnBuilder destinCol = col.column("Destination", "destination", type.stringType());
            TextColumnBuilder timesCol = col.column("Purchased times", "times", type.stringType());
            report.setTemplate(Templates.reportTemplate)
                    .columns(
                    monthCol.setHorizontalAlignment(HorizontalAlignment.CENTER),
                    scheNameCol.setHorizontalAlignment(HorizontalAlignment.CENTER),
                    originCol.setHorizontalAlignment(HorizontalAlignment.CENTER),
                    destinCol.setHorizontalAlignment(HorizontalAlignment.CENTER),
                    timesCol.setHorizontalAlignment(HorizontalAlignment.CENTER))
                    .title(cmp.horizontalList().add(
                    cmp.image(this.getClass().getResourceAsStream("/com/railway/image/logo.png")).setFixedDimension(192, 70),
                    cmp.text("Schedule Exception Report").setStyle(titleStyle).setHorizontalAlignment(HorizontalAlignment.LEFT),
                    cmp.text(new Date()).setStyle(titleStyle).setHorizontalAlignment(HorizontalAlignment.RIGHT)).newRow()
                    .add(cmp.filler().setStyle(stl.style().setTopBorder(stl.pen2Point())).setFixedHeight(10)))
                    .pageFooter(cmp.pageXofY());
//            ColumnGroupBuilder itemGroup = grp.group(monthCol);
//            report.groupBy(itemGroup);
            report.setDataSource(datasource);
            JasperPrint jsp = report.toJasperPrint();
            reportView = new JRViewer(jsp);
            reportView.setZoomRatio(0.8f);
            return reportView;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public JRViewer buildSummary() {
        try {
            StyleBuilder titleStyle = stl.style(Templates.boldCenteredStyle)
                    .setVerticalAlignment(VerticalAlignment.MIDDLE)
                    .setFontSize(13);
            JasperReportBuilder report = new JasperReportBuilder();
            TextColumnBuilder scheNameCol = col.column("Schedule Name", "scheduleName", type.stringType());
            TextColumnBuilder depTimeCol = col.column("Departure Time", "departureTime", type.stringType());
            TextColumnBuilder routeCol = col.column("Route Name", "routeName", type.stringType());
            TextColumnBuilder trainCol = col.column("Train Name", "trainName", type.stringType());
            TextColumnBuilder statusCol = col.column("Status", "status", type.stringType());
            report.setTemplate(Templates.reportTemplate)
                    .columns(
                    scheNameCol.setHorizontalAlignment(HorizontalAlignment.CENTER),
                    depTimeCol.setHorizontalAlignment(HorizontalAlignment.CENTER),
                    routeCol,
                    trainCol.setHorizontalAlignment(HorizontalAlignment.CENTER),
                    statusCol.setHorizontalAlignment(HorizontalAlignment.CENTER))
                    .title(cmp.horizontalList().add(
                    cmp.image(this.getClass().getResourceAsStream("/com/railway/image/logo.png")).setFixedDimension(192, 70),
                    cmp.text("Schedule Summary/Details Report").setStyle(titleStyle).setHorizontalAlignment(HorizontalAlignment.LEFT),
                    cmp.text(new Date()).setStyle(titleStyle).setHorizontalAlignment(HorizontalAlignment.RIGHT)).newRow()
                    .add(cmp.filler().setStyle(stl.style().setTopBorder(stl.pen2Point())).setFixedHeight(10)))
                    .pageFooter(cmp.pageXofY()).sortBy(asc(routeCol));
            ColumnGroupBuilder itemGroup = grp.group(routeCol);
            report.groupBy(itemGroup);
            report.setDataSource(datasource);
            JasperPrint jsp = report.toJasperPrint();
            reportView = new JRViewer(jsp);
            reportView.setZoomRatio(0.8f);
            return reportView;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    private JRDataSource buildDataSource(ArrayList<Map<String, Object>> data) {
        DRDataSource ds;
        Map<String, Object> col = data.get(0);
        String[] colNames = new String[col.size()];
        int z = 0;
        for (Map.Entry<String, Object> entry : col.entrySet()) {
            String string = entry.getKey();
            colNames[z] = string;
            z++;
        }
        ds = new DRDataSource(colNames);
        for (int i = 0; i < data.size(); i++) {
            Map<String, Object> map = data.get(i);
            Object[] obj = new Object[map.size()];
            for (int j = 0; j < obj.length; j++) {
                obj[j] = map.get(colNames[j]);
            }
            ds.add(obj);
        }
        return ds;
    }
}
